home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / hexfile.com / HEXFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-14  |  3.1 KB  |  155 lines

  1. #include <fcntl.h>
  2. #include <sys\types.h>
  3. #include <sys\stat.h>
  4. #include <stdio.h>
  5. #include <io.h>
  6. #include <stdlib.h>
  7. #include <errno.h>
  8. #include <string.h>
  9.  
  10.  
  11. #define version "1.0"
  12.  
  13. #define TRUE  1
  14. #define FALSE 0
  15.  
  16. #define INPUT_COUNT 16
  17. #define OUTPUT_COUNT INPUT_COUNT*5 + 2
  18.  
  19.  
  20.  
  21.  
  22. main(argc, argv)
  23. int  argc;
  24. char *argv[];
  25.  
  26. {
  27.    int  input_fnum, output_fnum;
  28.    int  i, cnt;
  29.    unsigned char buffer[INPUT_COUNT];
  30.    unsigned char outbuf[OUTPUT_COUNT+1];
  31.  
  32.      if (argc < 3)
  33.         {
  34.         cputs("Program HEXFILE; version ");
  35.         cputs(version);
  36.         cputs("\n\rProgram usage: hexfile <input file> <output file>\n");
  37.         return;
  38.         }
  39.        else
  40.         {
  41.           if (!strcmp(argv[1], argv[2]))
  42.              {
  43.              cputs("Error -- output file must different than input file");
  44.              return;
  45.              }
  46.  
  47.           if ((input_fnum = open(argv[1], O_RDONLY | O_BINARY)) == -1)
  48.              {
  49.              cputs("Error -- Unable to open input file");
  50.              return;
  51.              }
  52.  
  53.           /* see if the output file exists */
  54.           if ((output_fnum = 
  55.                open(argv[2], O_CREAT |  O_EXCL, S_IWRITE)) == -1)
  56.              {
  57.              if (errno == EEXIST)
  58.                 {
  59.                 cputs("The output file already exists. Write over it? ");
  60.                 i = getche();
  61.                 if (i != 'y' && i != 'Y')  exit(1);
  62.                 }
  63.              else
  64.                 {
  65.                 cputs("Error -- Unable to create the output file.\n\r");
  66.                 exit(1);
  67.                 }
  68.              }
  69.           remove(argv[2]);
  70.           output_fnum = 
  71.              open(argv[2], O_CREAT | O_BINARY | O_RDWR, S_IWRITE);
  72.           if (output_fnum == -1)
  73.              {
  74.              cputs("Error -- Unable to create the output file.\n\r");
  75.              return;
  76.              }
  77.  
  78.         }
  79.  
  80.  
  81.  
  82.  
  83.      while ((cnt = read(input_fnum, buffer, INPUT_COUNT)) > 0)
  84.          {
  85.          hexize(buffer, outbuf, &cnt);
  86.          write(output_fnum, outbuf, cnt);
  87.          }
  88.  
  89.      close(input_fnum);
  90.      close(output_fnum);
  91.      exit(1);
  92. }
  93.  
  94.  
  95.  
  96. hexize(char *in, char *out, int *cnt)
  97. {
  98.    int   i, j;
  99.    unsigned char *p, *q;
  100.  
  101.    p = in;
  102.    q = out;
  103.    for (i = *cnt; i > 0; i--, p++)
  104.        {
  105.        q[0] = '0';
  106.        q[1] = 'x';
  107.        q += 2;
  108.        itoh(*p, q);   /* this procedure always returns 2 chars + '\0' */
  109.        q[2] = ',';
  110.        q += 3;
  111.        }
  112.    q[0] = '\r';
  113.    q[1] = '\n';
  114.    *cnt = *cnt * 5 + 2;
  115. }
  116.  
  117.  
  118.  
  119.  
  120. itoh(n, s)        /* convert the byte 'n' to the string 's' */
  121. unsigned char s[];
  122. unsigned char n;
  123. {
  124.    int i, j, hold;
  125.  
  126.  
  127.    i = 0;
  128.  
  129.    do {                          /* generate digits in reverse order */
  130.       j = n % 16;
  131.       if (j < 10)
  132.           s[i++] = j + '0';
  133.       else
  134.           s[i++] = j - 10 + 'A';
  135.       }
  136.       while ((n /= 16) > 0);     /* delete it      */
  137.  
  138.    s[i] = '\0';
  139.    if (strlen(s) == 1)
  140.       {
  141.       s[1] = s[0];
  142.       s[0] = '0';
  143.       s[2] = '\0';
  144.       }
  145.    else
  146.       {
  147.       hold = s[0];
  148.       s[0] = s[1];
  149.       s[1] = hold;
  150.       }
  151. }
  152.  
  153.  
  154.  
  155.